package cn.orangelite.ai.controller;

import cn.orangelite.ai.domain.AiReplyConfig;
import cn.orangelite.ai.service.IAiReplyConfigService;
import cn.orangelite.common.core.utils.StringUtils;
import cn.orangelite.common.core.web.controller.BaseController;
import cn.orangelite.common.core.web.domain.AjaxResult;
import cn.orangelite.common.core.web.page.TableDataInfo;
import cn.orangelite.common.log.annotation.Log;
import cn.orangelite.common.log.enums.BusinessType;
import cn.orangelite.common.security.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/reply")
public class AiReplyConfigController extends BaseController {
    @Autowired
    private IAiReplyConfigService aiReplyConfigService;

    @RequiresPermissions("ai:reply:list")
    @GetMapping("/list")
    public TableDataInfo list(AiReplyConfig aiReplyConfig)
    {
        startPage();
        List<AiReplyConfig> list = aiReplyConfigService.list(
                Wrappers.<AiReplyConfig>lambdaQuery()
                        .like(StringUtils.isNotEmpty(aiReplyConfig.getQuestion()),AiReplyConfig::getQuestion,aiReplyConfig.getQuestion())
                        .orderByDesc(AiReplyConfig::getId));
        return getDataTable(list);
    }

    @RequiresPermissions("ai:reply:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(aiReplyConfigService.getById(id));
    }

    /**
     * 新增gptkey

     */
    @RequiresPermissions("ai:reply:add")
    @Log(title = "knowledge", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiReplyConfig aiReplyConfig) throws Exception {
        return toAjax(aiReplyConfigService.save(aiReplyConfig));
    }

    /**
     * 修改gptkey

     */
    @RequiresPermissions("ai:reply:edit")
    @Log(title = "knowledge", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiReplyConfig aiReplyConfig) throws Exception {
        return toAjax(aiReplyConfigService.updateById(aiReplyConfig));
    }

    /**
     * 删除gptkey

     */
    @RequiresPermissions("ai:reply:remove")
    @Log(title = "knowledge", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) throws Exception {
        return toAjax(aiReplyConfigService.removeBatchByIds(Arrays.asList(ids)));
    }

}
